var table = layui.table;
layui.use(['form','layer'],function() {
    var form = layui.form;
    layer = parent.layer === undefined ? layui.layer : top.layer;
        $ = layui.jquery;
    layui.use('table', function () {
        tableIns=table.render({
            elem: '#postList'
            , url: '/post/list'
            , method: 'post' //默认：get请求
            , request: {
                pageName: 'page' //页码的参数名称，默认：page
                , limitName: 'limit' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.totalRec, //解析数据长度
                    "data": res.data.recList //解析数据列表
                };
            }
            , cellMinWidth: 95
            , cols: [[
                {type: 'numbers', title: '编号'}
                , {type: 'checkbox'}
                , {field: 'name', title: '岗位名称', unresize: true, sort: true}
                , {field: 'descr', title: '岗位描述', unresize: true, sort: true}
                , {field: 'grade', title: '评分', unresize: true, sort: true, align: 'center',}
                , {fixed: 'right', title: '操作', width: 300, align: 'center', toolbar: '#bar'}
            ]]
            , page: true
            , id: 'postListTable'
        });
    });
    $('#searchTransport').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    var active ={
        reload: function(){
            var demoReload = $('#demoReload');
            //执行重载
            layui.table.reload('postListTable', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    name: demoReload.val()
                }
            }, 'data');
        }
    };
    layui.use('table', function () {
        var table = layui.table;
        //监听表格复选框选择
        table.on('checkbox(postList)', function (obj) {
            console.log(obj)
        });
        //监听工具条
        table.on('tool(postList)', function (obj) {
            var data = obj.data;
            if (obj.event === 'tableUpdate') {
                window.UpdateId = data.id;
                layer.open({
                    type: 2,
                    title: '岗位信息',
                    content: '/post/toEdit?id='+data.id,
                    shade: 0.5,
                    area : ["500px","320px"],
                    maxmin: true,
                    end: function(){
                        table.reload('postListTable');
                    }
                });
            }
            else if (obj.event === 'tableDelete') {
                layer.confirm('真的删除行么', function (index) {
                    obj.del();
                    $.ajax({
                        type: "post",
                        url: "/post/delete?id=" + data.id,
                        dataType: "json"
                    });
                    layer.close(layer.index);
                    var index = parent.layer.getFrameIndex(window.name);
                    layer.close(index);
                    this.parent.close();
                    layui.table.reload('postListTable');
                });
                //location.reload();
            }
        });
    });
});
function insert() {
    layer.open({
        type: 2,
        title: '添加岗位',
        content: '/post/toAdd',
        shade: 0.5,
        area : ["500px","320px"],
        maxmin: true,
        end: function(){
            table.reload('postListTable');
        }
    });
}
function delAll(){
    var checkStatus = layui.table.checkStatus('postListTable'),
        data = checkStatus.data,
        roleId = [];
    if (data.length > 0) {
        for (var i in data) {
            roleId.push(data[i].id);
        }
        console.log(roleId.join(','));
        layer.confirm('确定删除选中的岗位？', {icon: 3, title: '提示信息'}, function (index) {
            $.post("/post/toDelete", {
                id: roleId.join(',')  //将需要删除的newsId作为参数传入
            }, function (data) {
                layer.msg("岗位删除成功！", {icon: 2, time: 500});
                layui.table.reload('postListTable');
                layer.close(index);
                window.location.reload();
            })
        })
    } else {
        layer.msg("请选择需要岗位的角色");
    }
}